import json
import os
import shutil


def parse_json_file(file_path):
    result_dict = {}
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            try:
                data = json.loads(line)
                id_value = data.get('id')
                algorithm_mark = data.get('algorithmMark')[0]
                source = data.get('source')
                if algorithm_mark and id_value and source:
                    if algorithm_mark not in result_dict:
                        result_dict[algorithm_mark] = {}
                    if source not in result_dict[algorithm_mark]:
                        result_dict[algorithm_mark][source] = []
                    result_dict[algorithm_mark][source].append(id_value)
            except json.JSONDecodeError:
                print(f"解析行 '{line}' 时出错")
    return result_dict


def get_id_list(file_path):
    # 替换为你的实际文件路径
    # file_path = r'C:\Users\wanji\Desktop\fsdownload\truth.json'
    new_dict = parse_json_file(file_path)
    print(new_dict)
    id_list = []
    for key, value in new_dict.items():
        if key == '数据异常':
            continue
        for source, ids in value.items():
            n = 4
            # print(f"{key}: {source}")
            for v in ids:
                if n > 0:
                    n -= 1
                    # print(v)
                    id_list.append(v)
                else:
                    break

    print(len(id_list))
    return id_list


def get_json(d_path, save_path, id_list):
    for ID in id_list:
        old_path = os.path.join(d_path, "all_" + str(ID) + '.json')
        new_path = os.path.join(save_path, "all_" + str(ID)+'.json')
        if os.path.exists(old_path):
            shutil.copy(old_path, new_path)
        else:
            print(f"文件 {old_path} 不存在")


if __name__ == '__main__':
    file = r'C:\Users\wanji\Desktop\fsdownload\truth.json'
    id_list = get_id_list(file)
    d = r'D:\GJ\项目\事故检测\all_json'
    save = r'D:\GJ\项目\事故检测\ceshi'
    get_json(d, save, id_list)
